package com.xdap.init.dao;

import com.definesys.mpaas.query.MpaasQuery;
import com.xdap.api.moudle.datamodule.pojo.XdapDataModule;
import com.xdap.init.pojo.po.DataModel;
import com.xdap.init.pojo.po.DataModelField;
import com.xdap.motor.utils.StringUtils;
import com.xdap.runtime.service.RuntimeDatasourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

/**
 * @USER liShuShu
 * @TIME 2023/2/13 0013 16:49
 * @DESC 查询客户表
 **/
@Repository
@RequiredArgsConstructor
public class ModelDao {
    private final RuntimeDatasourceService runtimeDatasourceService;

    @Value("${spring.datasource.name}")
    private String tenantSchema;

    public DataModel getModel(String TableName){
        if (TableName == null || StringUtils.isBlank(TableName + "")) {
            return null;
        }

        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        mpaasQuery.eq("model_code", TableName);
        mpaasQuery.table("apaas_data_model");
        DataModel apaasDataModelFields = mpaasQuery.doQueryFirst(DataModel.class);
        return apaasDataModelFields;
    }
    public List<DataModel> getAllModel(){

        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        mpaasQuery.table("apaas_data_model");
        List<DataModel> apaasDataModelFields = mpaasQuery.doQuery(DataModel.class);
        return apaasDataModelFields;
    }

    public Object saveModel(String modelName) {

        MpaasQuery mpaasQuery = runtimeDatasourceService.buildTenantMpaasQuery(tenantSchema);
        mpaasQuery.table("apaas_data_model");
        DataModel dataModel = new DataModel();
        dataModel.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        dataModel.setModelName(modelName);
        return mpaasQuery.doInsert(dataModel);
    }
}
